﻿package Welsy4.Connect.vo
{
	import Welsy4.Connect.model.ConnectModel;
	import mx.controls.Alert;
	/**
	 * ...
	 * @author DefaultUser (Tools -> Custom Arguments...)
	 */
	public class HttpServiceHandler
	{
		public var xml:XML;
		
		
		public function HttpServiceHandler() 
		{
			
		}
		
		public function setXML(xmlobj: Object):void {
			
		    this.xml = XML(xmlobj);    	
		}
		
		public function toString():String {
		  
		   if (this.xml.debug.@status == "OK") {
		       return this.xml.debug.@status + " at " + this.xml.debug.@timestamp;
			   }
		return "HttpServiceHandler.toString() : Request HTTP success, but retuned an error string: " + this.xml ;
		}
		
		public function getInfo():String {
			if (this.xml.debug.@status == "OK") {
				return this.xml;
			}
			return "HttpServiceHandler.getInfo() : Request HTTP success, but retuned an error string: " + this.xml ;
		}
		
		public function getResultStatus():String {
		    
			if(this.xml.common.cookie.toString().length > 0){
			    ConnectModel.getInstance().sessionCookie = this.xml.common.cookie;
			}
			switch(this.xml.status.@code.toString()) {
			    case "ok":
						return ConnectModel.SESSION_OK;
				break;
				case "invalid":
				    return ConnectModel.INVALID_REQUEST;	
				break;
				case "no-data":
				    return ConnectModel.NO_DATA_RESPONSE;	
				break;
				case "no-access":
				    return ConnectModel.NO_ACCESS_RESPONSE;	
				break;
				case "too-much-data":
				    /* duplicated login! which user would log in? */
				    return ConnectModel.TOO_MUCH_DATA_RESPONSE;	
				break;
				default:
				    return ConnectModel.BAD_REQUEST;
				break;
			}
		}
		
		public function getResultStatusInfo():String {
			/* to use only in case of this.xml.status.@code != ok to explain */
		    return this.xml.status.@subcode;
		}
		/* */
		public function getMyMeetings():Array {
			
			var meetings:Array = new Array();
			
			for (var i:int = 0; i < this.xml.elements("my-meetings").meeting.length(); i++) {
					// store meeting object
					var m:Meeting = new Meeting();
					m.setMeeting(this.xml.elements("my-meetings").meeting[i]);
					meetings.push(m);
			}
				
			return meetings;	
		}
		
		public function getMeetingJustCreated():Meeting {
		    if (this.xml.sco.@type == "meeting") {
				var m:Meeting = new Meeting();
				m.setMeeting(XML(this.xml.sco));
				return m;
				}
			return null;	
		
		}
		
		/*
		 * 
		 * getSCOShortcuts() command function 
		 * 
		 * */
		public function getSCOShortcuts():Array {
			var SCOShortcuts:Array = new Array();
			
			for (var i:int = 0; i < this.xml.shortcuts.sco.length(); i++) {
			    SCOShortcuts.push(new SCOShortcut(this.xml.shortcuts.sco[i]));
			}
			
			return SCOShortcuts;
		}
		
		public function getSCOShortcutByType(type:String):SCOShortcut {
		    var SCOShortcuts:Array = getSCOShortcuts();
			
			for (var i:int = 0; i < SCOShortcuts.length; i++) {
				if (SCOShortcuts[i].type == type) return SCOShortcuts[i];
			}
			
			return null;
			
		}
		
		/**
		 * Restituisce il principal-id cercato
		 * @return principal-id String
		 */
		
		public function getPrincipalId():String {
		  return xml.elements("principal-list").principal.attribute("principal-id").toString();
		}
		
		public function getLastUserFound():User {
		    var u:User = new User(xml.elements("principal-list").principal.name);
			u.principal_id = getPrincipalId();
		    return u;
		}
		
		
	}
	
}